package com.wanxin.car.util.helper.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.wanxin.car.util.helper.test.Role;

/**
 * ͨ��ģ��������
 * @author lqk
 *
 */
public class CodeHelperTemplateImpl extends CodeHelperBase {
	
	//ģ����
	private static final String TAG_FOR = "@{foreach}";
	private static final String TAG_FOR_END = "@{/foreach}";
	private static final String TAG_FIELD = "@{field}";
	private static final String TAG_ACTION = "@{action}";

	/**
	 * ���ʵ���jsp¼���޸�ҳ��
	 * @param clazz
	 * @param aciton struts2ӳ���action���
	 * @return ����ȡģ��ʧ�ܷ���null
	 */
	public String printEntityJspInput(Class entityClass, String aciton) {
		
		// ��ȡģ���е�����
		String tmp = getTmp("tmpinput.jsp");
		if( tmp==null ) return null;
		
		List<String> foreachTmps = getForeachTmps(tmp);
		
		List<String> foreachs = new ArrayList<String>();//���foreachģ����ɵ�����
		List<String> fieldNames = getFieldNames(entityClass);// ʵ���������
		for (String fortmp : foreachTmps) {
			
			StringBuilder foreach = new StringBuilder();
			for (int i = 0; i<fieldNames.size(); i++) {
				String name = fieldNames.get(i);
				if( name.equals("id") ){
					//��id�������⴦��,�����id��
				} else {
					String foreachstr = fortmp.replace(TAG_FIELD, name);
					foreach.append(foreachstr).append("\n");
				}
				
			}
	
			foreachs.add(foreach.toString());
		}
		
		//ԭ��ģ���滻@{action}
		tmp = tmp.replace(TAG_ACTION, aciton);
		
		//�滻foreach
		tmp = replaceForeachs(tmp,foreachs);
		return tmp;
	}
	
	/**
	 * ����ģ�������ʾʵ���б��jsp
	 * @param aciton struts2ӳ���action���
	 * @return ����ȡģ��ʧ�ܷ���null
	 */
	public String printEntityJspList(Class entityClass, String aciton) {
	
		// ��ȡģ���е�����
		String tmp = getTmp("tmplist.jsp");
		if (tmp == null)
			return null;
	
		//��ȡҪѭ����������foreach����
		List<String> foreachTmps = getForeachTmps(tmp);
		
		List<String> foreachs = new ArrayList<String>();//���foreachģ����ɵ�����
		List<String> fields = getFieldNames(entityClass);// ʵ�������
		for (String fortmp : foreachTmps) {
			
			StringBuilder foreach = new StringBuilder();
			for (int i = 0; i<fields.size(); i++) {
				String f = fields.get(i);
				String foreachstr = fortmp.replace(TAG_FIELD, f);
				foreach.append(foreachstr).append("\n");
			}
	
			foreachs.add(foreach.toString());
		}
		
		//�滻action
		tmp = tmp.replace(TAG_ACTION, aciton);
		
		//�滻foreach
		tmp = replaceForeachs(tmp,foreachs);
		return tmp;
	}

	/**
	 * ��ȡģ����foreachѭ������,��ȥ����β����
	 */
	private List<String> getForeachTmps( String tmp ){
		
		List<String> result = new ArrayList<String>();
		
		while( true ){
			int beginIndex = tmp.indexOf(TAG_FOR);
			int endIndex = tmp.indexOf(TAG_FOR_END);
			
			if( beginIndex==-1 ){
				break;
			}
			
			String fortmp = tmp.substring(tmp.indexOf(TAG_FOR)+TAG_FOR.length(),tmp.indexOf(TAG_FOR_END) );
			fortmp = trim(fortmp, 0);
			result.add(fortmp);
			
			tmp = tmp.substring(endIndex+TAG_FOR_END.length(), tmp.length());
		}
		
		return result;
		
	}
	
	/**
	 * ȥ��tab ���е��ַ�
	 * @param flag -1 ֻȥ����� 1 ֻȥ����� 0��β��ȥ��
	 */
	private String trim( String s,int flag ){
		
		int begin = 0;
		int end = s.length();
		
		if( flag==0 || flag==-1 ){
			for (int i = 0; i < s.length(); i++) {
				if( s.charAt(i) == '\n' ){
					begin = i+1;
				} else {
					break;
				}
			}
		}
		
		if( flag==0||flag==1 ){
			for( int i=(s.length()-1) ;i>=0 ;i-- ){
				
				char c = s.charAt(i);
				if( c=='\n' || c=='\t'){
					end = i;
				} else {
					break;
				}
			}
		}
		return s.substring(begin, end);
		
	}
	
	/**
	 * ȥ��ʼ��tab
	 */
	private String triml( String s ){
		int begin = 0;
		for (int i = 0; i < s.length(); i++) {
			if( s.charAt(i) == '\t' ){
				begin = i+1;
			} else {
				break;
			}
		}
		return s.substring(begin);
	}
	
	/**
	 * ȥ��ĩβ��tab ����
	 */
	private String trimr( String s ){
		int end = s.length();
		for( int i=(s.length()-1) ;i>=0 ;i-- ){
			char c = s.charAt(i);
			if( c=='\n' || c=='\t'){
				end = i;
			} else {
				break;
			}
		}
		return s.substring(0,end);
	}

	/**
	 * ��foreachs�������滻��ģ����ȥ
	 * @param tmp
	 * @param foreachs
	 * @return 
	 */
	private String replaceForeachs( String tmp, List<String> foreachs ){
		
		int idx = 0;
		while( true ){
			int beginIndex = tmp.indexOf(TAG_FOR);
			
			if( beginIndex==-1 ){
				break;
			}
			
			String fortmp = tmp.substring( beginIndex,tmp.indexOf(TAG_FOR_END)+TAG_FOR_END.length() );
			String foreach = foreachs.get(idx++);
			foreach = triml(foreach);
			foreach = trimr(foreach);
			tmp = tmp.replace(fortmp, foreach);
		}
		return tmp;
	}

	/**
	 * ��ȡ�ı�����,ʧ�ܷ���null
	 * @return 
	 */
	private String getTmp(String name) {

		Resource tmpres = new ClassPathResource(name);
		StringBuilder tmp = new StringBuilder();
		BufferedReader in = null;
		try {
			in = new BufferedReader(new InputStreamReader(tmpres.getInputStream()));
			String line = null;
			while ((line = in.readLine()) != null) {
				tmp.append(line).append("\n");
			}
			return tmp.toString();

		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (in != null)
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}


	public static void main(String[] args) {
		CodeHelperTemplateImpl h = new CodeHelperTemplateImpl();
		String s = h.printEntityJspInput(Role.class, "peop");
		System.out.println(s);
	}
}
